package com.aoip.telemetry.telemetry.configureui.business.common.ad.controller;


import com.aoip.telemetry.telemetry.configureui.business.common.ad.service.AttAdBaseService;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.common.AttAdBaseEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 行政区划工具类
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/02/09
 */
@RestController
public class AttAdBaseController extends BaseController {

    @Value("${aoip.adCode}")
    private String adCode;

    @Resource
    private AttAdBaseService service;

    /**
     * 省市县三级联动组件树结构数据
     *
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/2/9
     **/
    @GetMapping(value = "/v1/GET/ad/list")
    public AjaxResult list() {
        List<AttAdBaseEntity> adList = service.regionDataPlusList();
        List<AttAdBaseEntity> cityList = new ArrayList<>();
        List<AttAdBaseEntity> countList = new ArrayList<>();
        AttAdBaseEntity provData = new AttAdBaseEntity();
        AttAdBaseEntity cityData = new AttAdBaseEntity();
        List<AttAdBaseEntity> list = new ArrayList<>();
        for (AttAdBaseEntity vo : adList) {
            String curAdCode = vo.getAdCode();
            if (this.adCode.equals(curAdCode) || "0000".equals(curAdCode.substring(2, 6))) {
                cityList = new ArrayList<>();
                provData = vo;
                list.add(vo);
                continue;
            }
            if ("00".equals(curAdCode.substring(4, 6))) {
                countList = new ArrayList<>();
                cityList.add(vo);
                cityData = vo;
                provData.setChildren(cityList);
            } else {
                countList.add(vo);
                cityData.setChildren(countList);
            }
        }
        return AjaxResult.success(list);
    }
}
